/** ***************************************************************************
 * CoffeeExyst.hpp
 * 2012 CoffeeParty Ltd.
 * Date Created: 2012-07-21                                  format YYYY-MM-DD
 * Revision Id: $Id$
 ******************************************************************************
 */

#ifndef _COFFEE_EXYST_HPP_
#define _COFFEE_EXYST_HPP_

#include "CoffeeCommons.hpp"
#include "CoffeeTypes.hpp"

class CoffeeExyst
{
public:
    CoffeeExyst()
    : referenceId(1) { }

    virtual ~CoffeeExyst() { }

    void hook() const
    {
        ++referenceId;
    }

    bool unhook() const
    {
        try_to_break_if(referenceId <= 0);

        --referenceId;
        if (!referenceId)
        {
            delete this;
            return true;
        }

        return false;
    }

    int getReferenceId() const
    {
        return referenceId;
    }

protected:
    mutable int referenceId;
};

#endif /** _COFFEE_EXYST_HPP_ */
